import React from 'react';
import { Icon, Input, Button } from 'antd';
import { withRouter } from 'react-router';
import auth from '../routes/auth.js';

export default class Login extends React.Component {

  constructor(props) {
    super(props);

  }
  login(){
    event.preventDefault();

    const email = this.refs.email.refs.input.value;
    const pass = this.refs.pass.refs.input.value;
    
    auth.login(email, pass, (loggedIn) => {
      console.log(loggedIn)
      if (!loggedIn)
        return this.setState({ error: true })

      const { location } = this.props

      if (location.state && location.state.nextPathname) {
        this.props.router.replace(location.state.nextPathname)
      } else {
        this.props.router.replace('/')
      }
    })
  }
  render() {
    return (

      <div className="login-box">
        <div>
          <Input ref="email" addonBefore={<Icon type="user" />} placeholder="Username" defaultValue="test@supermap.com"/>
        </div>
        <div>
          <Input ref="pass" addonBefore={<Icon type="lock" />} type="password" placeholder="Password" defaultValue="testtest" />
        </div>
        <Button type="primary" className="login-form-button" onClick={this.login.bind(this)}>
            登录
          </Button>
      </div>
    )
  }
}

